/**
 * 整车平台---修改设计的JS文件
 */

$(document).ready(function(){
	nowColorControl();//车身颜色选择
	editControl();//整车名称编辑修改按钮
	rightFormSubmit();//右侧细节设置修改提交
});

function getCarList(cartype){
     $("#loading").showLoading(); 
	  $.ajax({  
	        type : "POST",  
	        url : CONTEXT_PATH+'front/carDesign/getCarList?cartype='+cartype,  
	        dataType : "json",  
	        cache : false,  
	        async : false,  
	        success : function(data) {  
                $(".left-oneImgList.listImgBox").empty();
                
                for(i=0;i<data.length;i++){
                    $(".left-oneImgList.listImgBox").append(
        					"<div class='left-oneImgBlock leftImgBlock "+(data.id==$("#carid").val()? 'activeImg':'')+"'>"+
        					"<div class='left-oneImgBox'><img src='"+FILESERVER_PATH+"?id="+data[i].carsidepic+"'/></div>"+
        					"<div class='left-oneImgName'>"+data[i].name+"</div>"+
        				        "<input type='hidden' value="+JSON.stringify(data[i])+">"+
        				    "</div>");
                }
                imgSelectControl();
                $('#loading').hideLoading();
                
	        }  
	    }); 
	
}

function nowColorControl(){
	var $body = $(document.body);
	
	$(".rightForm-carColor").on("click",function(){
		var thisItem=$(this);
		
		$("#carcorlor").val(thisItem.find("input").val());
		
		if(!(thisItem.hasClass("nowColor"))){
			thisItem.addClass("nowColor");
			thisItem.siblings().removeClass("nowColor");
		}
	});
	
	$("#carSideBlack").click(function(){
		 $body.attr('class','client-body clm-body blackActive');
	});
	$("#carSideWhite").click(function(){
		 $body.attr('class','client-body clm-body whiteActive');
	});
	$("#carSideRed").click(function(){
		 $body.attr('class','client-body clm-body redActive');
	});
	$("#carSideBlue").click(function(){
		 $body.attr('class','client-body clm-body blueActive');
	});
}

function editControl(){
	$("#carNameEdit").on("click",function(){
		var thisItem=$(this);
		var itemParent=thisItem.parent();
		
		itemParent.css("display","none");
		itemParent.next().css("display","block");
		
		var carName=$("#carName").text();
		$("#carNameInput").val(carName);
		$("#alias").val(carName);
	});
	
	$("#carNameSure").on("click",function(){
		var thisItem=$(this);
		var itemParent=thisItem.parent();
		
		itemParent.css("display","none");
		itemParent.prev().css("display","block");
		
		var carNameInput=$("#carNameInput").val();
		$("#carName").text(carNameInput);
		$("#alias").val(carNameInput);
	});
}

function rightFormSubmit(){
	$("#formSubmit").click(function(){
		$("#carModifyModal").modal('show');
	});
	
}

var isSubmit = false; 

function submitCarDesignform(id){
	   if (isSubmit) {  
	        return  
	   }  
	                  
	  var  url;
	  
	  if(id==''){
		  url=CONTEXT_PATH+'front/carDesign/addCarDesign';
	  }else{
		  
		  url=CONTEXT_PATH+'front/carDesign/updateCarDesign';
	  }
	   $("#carModifyModal").modal('hide');
	   $("#loading").showLoading(); 
	   isSubmit = true;  
	   $('#carDesignform').ajaxSubmit({
		    dataType: "json",  
		    url: url,
		    type : "POST",  
		    success: function(data) {  
		    	$('#loading').hideLoading();
                if(data.result){
                 
                	 isSubmit = false;  
                     window.location.href= CONTEXT_PATH+'front/carDesign/carDesign_list';
                	
                } 
		    }   
		  
	  });  
}

function submitCarLogoModify(id){
	editor.sync('content');
    $("#loading").showLoading(); 
	$("#carModifyModal").modal('hide');
	$('#carLogoform').ajaxSubmit({
	    dataType: "json",  
	    url: "front/carDesign/updateCarDesign",
	    type : "POST",  
	    success: function(data) {  
	    	$('#loading').hideLoading();
            if(data.result){
            	//self.location=document.referrer;
            	  window.location.href= CONTEXT_PATH+'front/carDesign/carDesignModify?id='+id;
             }

	    }   
	  
  });  
 
             	
    
}

function download(designid){
        
	    $("#designid").val(designid);
		$("input[name='chose']").prop("checked",false);
		$("#loadModal").modal('show');
	 
}

function submitDownload(){
	
	if ($("input[name='chose']:checked").length!=0) {
		$("#downloadForm").submit();
		$("#loadModal").modal('hide');
	}else{
		jAlert("请先至少选中一个","提示");
	}

 
}
